<?php

namespace app\store\service;

use app\common\model\Evaluation;
use app\common\model\Order;
use app\common\model\Product;
use app\common\model\User;
use app\Response;


class OrderService
{
    /** 订单列表
     * @param $user_id int 用户ID
     * @param $tid int 抖音号ID
     * @param $page int 页码
     * @param $num int 个数
     * @return array1
    */
    public static function orderListService($user_id,$page,$num,$state,$type){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[24])){
            return Response::wrapError(0,"账户非门店用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $where[] = ["o.enable","=",1];
        $where[] = ["o.stid","=",$userInfo['stid']];
        if(is_numeric($state)){
            $where[] = ["o.state","=",$state];
        }else{
            $where[] = ["o.state","<>",0];
        }
        $where[] = ["o.type","=",$type];
        $field = "o.*,p.img";
        $list = Order::orderList($where,$page,$num,$field);
        if(!empty($list)){
            $data = $list->toArray();
            foreach ($data as $k => $v){
                $data[$k]["addtime"] = !empty($v["addtime"])?date("Y-m-d H:i:s",$v["addtime"]):"";
            }
            $count = Order::orderListCount($where);
        }else{
            $data = [];
            $count = 0;
        }
        return Response::wrapData($data,$count);
    }

    /*** 订单详情
     * @param $id int
     * @param $user_id int
     * @return Json
     */
    public static function orderInfoService(int $id, int $user_id){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[24])){
            return Response::wrapError(0,"账户非门店用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $where[] = ["enable","=",1];
        $where[] = ["id","=",$id];
        $where[] = ["stid","=",$userInfo['stid']];
        $info = Order::dataInfo($where);
        if(empty($info)){
            return Response::wrapError(0,"未检测到当前订单信息!");
        }
        $p_info = Product::dataInfo(["id"=>$info["p_id"]]);
        $e_info = Evaluation::dataInfo(["oid"=>$id]);
        if(!empty($p_info)){
            $info["p_info"] = $p_info;
            $info["addtime"] = !empty($info["addtime"])?date("Y-m-d H:i:s",$info["addtime"]):"";
            $info["paytime"] = !empty($info["paytime"])?date("Y-m-d H:i:s",$info["paytime"]):"";
            $info["sendtime"] = !empty($info["sendtime"])?date("Y-m-d H:i:s",$info["sendtime"]):"";
            $info["endtime"] = !empty($info["endtime"])?date("Y-m-d H:i:s",$info["endtime"]):"";
            $info["ev_time"] = !empty($e_info["addtime"])?date("Y-m-d H:i:s",$e_info["addtime"]):"";
        }else{
            $info["p_info"] = [];
        }
        return Response::wrapData($info);
    }



    public static function ewmOrderInfoService($user_id,$data){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[24])){
            return Response::wrapError(0,"账户非门店用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        if(empty($data)){
            return Response::wrapError(0,"未提取到二维码信息!");
        }
        if(is_string($data)){
            $data = object_array(json_decode($data));
        }
        $where[] = ["id","=",$data["order_id"]];
        $where[] = ["o_pro_num","=",$data["order_pro"]];
        $where[] = ["stid","=",$userInfo["stid"]];
        $info = Order::dataInfo($where);
        if(empty($info)){
            return Response::wrapError(0,"二维码信息异常!");
        }
        $key = $info["id"].$info["o_pro_num"].$info["uid"].$info["addtime"];
        if($data["key"] != md5($key)){
            return Response::wrapError(0,"信息校验失败!");
        }
        $p_info = Product::dataInfo(["id"=>$info["p_id"]]);
        if(!empty($p_info)){
            $info["p_info"] = $p_info;
            $info["addtime"] = !empty($info["addtime"])?date("Y-m-d H:i:s",$info["addtime"]):"";
            $info["paytime"] = !empty($info["paytime"])?date("Y-m-d H:i:s",$info["paytime"]):"";
            $info["sendtime"] = !empty($info["sendtime"])?date("Y-m-d H:i:s",$info["sendtime"]):"";
            $info["endtime"] = !empty($info["endtime"])?date("Y-m-d H:i:s",$info["endtime"]):"";
        }else{
            $info["p_info"] = [];
        }
        return Response::wrapData($info);
    }

    /*** 订单确认收货
    */
    public static function orderReceiveService($user_id,$id){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[24])){
            return Response::wrapError(0,"账户非门店用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $where[] = ["enable","=",1];
        $where[] = ["id","=",$id];
        $where[] = ["stid","=",$userInfo['stid']];
        $info = Order::dataInfo($where);
        if(empty($info)){
            return Response::wrapError(0,"未检测到当前订单信息!");
        }
        if($info["state"] != 2){
            return Response::wrapError(0,"订单不在可收货状态!");
        }
        $res = Order::editInfo(["id"=>$id],["state"=>3,"endtime"=>time()]);
        if($res){
            return Response::wrapData();
        }else {
            return Response::wrapError(0, "操作失败!");
        }
    }


}